import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/stores'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('../views/login/LoginIndex.vue') },
    { path: '/layout', component: () => import('../views/layout/LayoutIndex.vue') },
    {
      path: '/',
      component: () => import('@/views/layout/LayoutIndex.vue'),
      redirect: '/user',
      children: [
        { path: '/user', component: () => import('@/views/user/UserManager.vue') },
        { path: '/data', component: () => import('@/views/data/DataIndex.vue') },
        { path: '/setting', component: () => import('@/views/setting/SettingIndex.vue') },
        { path: '/control', component: () => import('@/views/control/ControlIndex.vue') },
        { path: '/judge', component: () => import('@/views/judge/JudgeIndex.vue') },
        { path: '/chat_ai', component: () => import('@/views/chat/ChatIndex.vue') },
      ],
    },
    { path: '/map', component: () => import('@/views/map/MapIndex.vue') },
  ],
})

//路由前置守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const token = userStore.getToken()
  //除/login外，都需要token
  if (to.path != '/login' && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router
